﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace _3
{
    public partial class Form1 : Form
    {
        private string _fileName;
        private string _content;
        private int _originalLineCount = 0;
        private int _originalWordCount = 0;
        private int _newLineCount = 0;
        private int _newWordCount = 0;
        private Dictionary<string, int> _wordCount = new Dictionary<string, int>();
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "C#文件|*.cs";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                label1.Text = openFileDialog.FileName;
                _fileName = openFileDialog.FileName;
                _content = System.IO.File.ReadAllText(_fileName);
                _originalLineCount = _content.Split('\n').Length;
                string cleanedInput = Regex.Replace(_content, @"[^\w\s]", "");
                _originalWordCount =
                    cleanedInput
                        .Split(new[] { ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)
                        .Count(word => !string.IsNullOrWhiteSpace(word));
                var message = $"清理前的文件{_fileName}的行数为：\n{_originalLineCount}\n文件{_fileName}的单词数为：\n{_originalWordCount}";
                MessageBox.Show(message);
            }
        }


        private void UpdateContent()
        {
            var _newContent = _content
                .Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)//去除空行
                .Where(lines => !string.IsNullOrWhiteSpace(lines)&&!lines.TrimStart().StartsWith("//"))//去除注释以及空格
                .Select(lines => lines)//选择
                .ToArray();
            _content = string.Join("\n", _newContent);
            _newLineCount = _content.Split('\n').Length;
            string cleanedInput = Regex.Replace(_content, @"[^\w\s]", "");
            _newWordCount =
                cleanedInput
                    .Split(new[] { ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)
                    .Count(word => !string.IsNullOrWhiteSpace(word));
            var message = $"清理后的文件{_fileName}的行数为：\n{_newLineCount}\n文件{_fileName}的单词数为：\n{_newWordCount}";
            MessageBox.Show(message);
            try
            {
                System.IO.File.WriteAllText(_fileName, _content);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

        }

        private void getWordCount(string content)
        {
            var words = content.Split(new[] { ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var word in words)
            {
                if (_wordCount.ContainsKey(word))
                {
                    _wordCount[word]++;
                }
                else
                {
                    _wordCount.Add(word, 1);
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process.Start("notepad.exe", _fileName);
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            getWordCount(_content);
            var sortedWordCount = _wordCount.OrderByDescending(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
            var message2 = sortedWordCount.Aggregate("", (current, word) => current + $"{word.Key} : {word.Value}\n");
            MessageBox.Show(message2);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            UpdateContent();
        }
    }
}